package com.atguigu.tingshu.order.context;

import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Author  王东剑
 * Created on 21/6/2024 PM6:42
 * Package_name com.atguigu.tingshu.order.context
 * Description
 */
@Component
@Slf4j
public class OrderTradeStrategyContext implements ApplicationContextAware {
	private static final ConcurrentHashMap<String , OrderTradeStrategy> orderTradeStrategyContextMap = new ConcurrentHashMap<>();
	public static OrderTradeStrategy getOrderTradeStrategy(String itemType){
		return orderTradeStrategyContextMap.get(itemType);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, OrderTradeStrategy> beansOfType = applicationContext.getBeansOfType(OrderTradeStrategy.class);
		beansOfType.values().forEach(orderTradeStrategy -> orderTradeStrategyContextMap.put(orderTradeStrategy.getItemType(),orderTradeStrategy));
	}
}
